package org.dregs.repository.impl;

import org.dregs.exception.ParamIsNullException;
import org.dregs.pojo.FileType;
import org.dregs.pojo.Version;
import org.dregs.repository.DataMapper;
import org.dregs.repository.IndexMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class IndexMapperImpl extends DataMapper implements IndexMapper {

    private final String TABLE_NAME_VERSION = "`version`";

    private final String TABLE_NAME_TYPE = "`file_type`";

    private final RowMapper<FileType> fileTypeRowMapper = (resultSet ,i)->{return new FileType(resultSet);};

    private final RowMapper<Version> versionRowMapper = (resultSet , i)->{return new Version(resultSet);};


    @Override
    public List<Version> queryAllVersion() {
        return queryList(
                format("SELECT * FROM %s",TABLE_NAME_VERSION),
                versionRowMapper);
    }

    @Override
    public List<FileType> queryAllFileType() {
        return queryList(
                format("SELECT * FROM %s",TABLE_NAME_TYPE),
                fileTypeRowMapper);
    }

    @Override
    public Version queryByMD5(String md5) {
        return queryObject(
                format("SELECT * FROM %s WHERE `md5` = ?",TABLE_NAME_VERSION),
                new Object[]{md5},
                versionRowMapper);
    }

    @Override
    public Version queryCurrentUse(String file_id) {
        return queryObject(
                format("SELECT * FROM %s WHERE `current` = 1 AND `file_id` = ?",TABLE_NAME_VERSION),
                new Object[]{file_id},
                versionRowMapper);
    }

    @Override
    public int insertFileType(FileType file_type) throws ParamIsNullException {
        return insertRecord(TABLE_NAME_TYPE,file_type);
    }

    @Override
    public int insertVersion(Version version) throws ParamIsNullException {
        return insertRecord(TABLE_NAME_VERSION,version);
    }

    @Override
    public int updateFileType(FileType file_type) throws ParamIsNullException {
        return updateRecord(TABLE_NAME_TYPE,file_type);
    }

    @Override
    public int updateVersion(Version version) throws ParamIsNullException {
        return updateRecord(TABLE_NAME_VERSION,version);
    }

    @Override
    public int deleteFileType(FileType file_type) throws ParamIsNullException {
        return deleteRecord(TABLE_NAME_TYPE,file_type);
    }

    @Override
    public int deleteVersion(Version version) throws ParamIsNullException {
        return deleteRecord(TABLE_NAME_VERSION,version);
    }

    @Override
    public int deleteFileTypeByID(long file_type_id) throws ParamIsNullException {
        return deleteRecord(TABLE_NAME_TYPE,file_type_id);
    }

    @Override
    public int deleteVersionByID(long version_id) throws ParamIsNullException {
        return deleteRecord(TABLE_NAME_VERSION,version_id);
    }

    private String format(String templet,String...strings){
        return String.format(templet,strings);
    }
}
